package view.plugin
{
	//用户图像
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.*;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	import global.Resource;
	
	import libs.bulkloader.BulkLoader;
	import libs.bulkloader.BulkProgressEvent;
	
	import util.RandomUtil;
	
	import vo.UserInfo;
	
	public class UserImage extends Sprite
	{
		public var userInfo:UserInfo = null;
		public var image:DisplayObject = null;
		public var load:Loader = null;
		public function UserImage()
		{
		}
		
		public function loader(userInfo:UserInfo):void
		{
			if(userInfo != null && this.userInfo == userInfo) return;
			this.userInfo = userInfo;
			if(userInfo.szImageUrl == null) 
			{
				trace("图像地址为空!");
				createDefaultImage(); return;
			}
			this.load = new Loader();
			this.load.load(new URLRequest(userInfo.szImageUrl));
//			this.load.load(new URLRequest("http://mopimg.cn/photos/0/0/women_tiny.gif"));
			this.load.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onLoaderComplete);
			this.load.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.onError);
		}
		private function createDefaultImage():void
		{
			if(image && image.parent)
				image.parent.removeChild(image);
			image = Resource.i.getMovieClip("common","HeadImage");
			if(!image) return;
			addChild(image);
			(image as MovieClip).gotoAndStop(userInfo.cbGender+1);
			dispatchEvent(new Event(Event.COMPLETE));
		}
		private function onError(evt:ErrorEvent):void
		{
			trace("IO错误!");
			createDefaultImage();
		}
		private function onLoaderComplete(evt:Event):void
		{
			this.load.contentLoaderInfo.removeEventListener(Event.COMPLETE, this.onLoaderComplete);
			this.load.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, this.onError);
			if(image && image.parent)
				image.parent.removeChild(image);
			this.addChild(image = load);
			image.width = image.height = 50;
			dispatchEvent(new Event(Event.COMPLETE));
		}
		public function get imageBitmap():DisplayObject
		{
			return this.image;
		}
		public function reset():void
		{
			userInfo = null;
			image = null;
			while(numChildren > 0) removeChildAt(0);
		}
	}
}